package helipay.api.service.impl;

import helipay.api.cache.dao.MerchantCacheDao;
import helipay.api.common.exception.BaseAPIException;
import helipay.api.dao.MerchantDao;
import helipay.api.dao.OrdersDao;
import helipay.api.dao.OrdersSwipeCardDao;
import helipay.api.jdbc.ListUtil;
import helipay.api.jedis.RedisAPI;
import helipay.api.model.Merchant;
import helipay.api.model.Orders;
import helipay.api.model.OrdersSwipeCard;
import helipay.api.service.MerchantService;
import helipay.api.util.Constant;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yimi on 2017/6/2.
 */
@Service
public class MerchantServiceImpl implements MerchantService {

    @Autowired
    private MerchantDao merchantDao;
    @Autowired
    private MerchantCacheDao merchantCacheDao;

    @Autowired
    private OrdersDao ordersDao;

    @Autowired
    private OrdersSwipeCardDao ordersSwipeCardDao;


    @Override
    public Map<String, Object> login(String customNumber, String loginPassword) {

        Map<String,Object> map = new HashMap<String,Object>();

        Merchant merchant = merchantDao.login(customNumber, loginPassword);
        if(merchant == null){
            throw new BaseAPIException("账号不存在或密码错误" +
                    "!");
        }
        map.put("customerNumber",customNumber);
        map.put("customerName",merchant.getName());
        String uuid = Constant.getUUID();
        merchantCacheDao.saveEx(RedisAPI.Db.FOUR.toIntValue(),"LOGIN_"+customNumber, uuid);
        map.put("session",uuid);
        return map;
    }

    @Override
    public String changePassword(String customerNumber, String newPassword,String oldPassword) throws SQLException {
        Merchant merchant=merchantDao.getMerchant(customerNumber);
        if(merchant==null){
            throw new BaseAPIException("商户号不存在！");
        }else{
            if(!oldPassword.equals(merchant.getLoginPassword())){
                throw new BaseAPIException("原始密码错误！");
            }else{
              //  merchantDao.update(merchant);
                merchantDao.changePassword(NumberUtils.toLong(merchant.getId()+""),newPassword);
            }

        }
        return "成功修改密码！";
    }

    @Override
    public List<Merchant> merchantList() {
       // List<Merchant> list = merchantDao.merchantList();
        return merchantDao.merchantList();
    }

    @Override
    public Map<String, Object> analysis(String customerNumber) {

        Map<String,Object> map = new HashMap<String,Object>();
        //DOUBLE 保留2位小数
        DecimalFormat    df   = new DecimalFormat("######0.00");
        //统计刷卡金额
        List<OrdersSwipeCard> ordersSwipeCardList =  ordersSwipeCardDao.orderSwipeCardListByCustomerNumber(customerNumber);
        if(ListUtil.isEmpty(ordersSwipeCardList)||ordersSwipeCardList.size()==0){
            map.put("swipeCardAmount",0);
        }else{
            Double swipeCardAmount = 0d;
            for (OrdersSwipeCard o:ordersSwipeCardList) {
                if(o.getBizType().equals("0")){
                    swipeCardAmount = swipeCardAmount + o.getOrderAmount();
                }else{
                    swipeCardAmount = swipeCardAmount - o.getOrderAmount();
                }
            }
            map.put("swipeCardAmount",df.format(swipeCardAmount));
        }
        //统计微信，支付宝金额
        List<Orders> ordersList = ordersDao.orderListByCustomerNumber(customerNumber);
        if(ListUtil.isEmpty(ordersList)||ordersList.size()==0){
            map.put("aliAmount",0);
            map.put("weChatAmount",0);
        }else{
            Double aliAmount = 0d;
            Double weChatAmount = 0d;
            for (Orders o:ordersList) {
                if(o.getAppType().equals("WXPAY")){
                    weChatAmount = weChatAmount + o.getOrderAmount();
                }else{
                    aliAmount = aliAmount + o.getOrderAmount();
                }
            }
            map.put("aliAmount",df.format(aliAmount));
            map.put("weChatAmount",df.format(weChatAmount));
        }

        return map;
    }


}
